#include <QDoubleSpinBox>
#include "ItemDouble.h"

double ItemDouble::get()
{
    return m_data;
}

void ItemDouble::set(const double &value)
{
    m_data = value;
    emit itemEdited();
}

QWidget *ItemDouble::createWidget(WidgetStyle s)
{
    Q_UNUSED(s)
    auto w = new QDoubleSpinBox;
    w->setRange(0, 9999);
    w->setMinimumWidth(180);
    w->setMaximumWidth(200);
    connect(w, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &ItemDouble::onWidgetEditedWrapper);
    return w;
}

void ItemDouble::update(QWidget *widget, UpdateOption option)
{
    QDoubleSpinBox *spinBox = qobject_cast<QDoubleSpinBox*>(widget);
    if (spinBox != nullptr)
    {
        if (option == UpdateOption::WidgetContent)
        {
            spinBox->setValue(m_data);
        }
        else if (option == UpdateOption::Item)
        {
            this->m_data = spinBox->value();
            qInfo() << "update QDoubleSpinBox: " << this->m_data;
        }
    }
    else
    {
        qWarning() << "";
    }
}

void ItemDouble::writeToData(QDataStream &stream) const
{
    stream << m_data;
}

void ItemDouble::readFromData(QDataStream &stream)
{
    stream >> m_data;
    emit itemEdited();
}
